package cn.netjava.robo3d.event;

import java.util.List;

import cn.netjava.robo3d.TankInformation;

import com.jme.math.Vector3f;
import com.jmex.effects.particles.ParticleMesh;

public class HitByFireEvent {

	private List<TankInformation> allothertanksinfo;       //引用所有其它坦克
	private TankInformation info;
	
	public HitByFireEvent(TankInformation info,List<TankInformation> allothertanksinfo){
		this.info=info;
		this.allothertanksinfo=allothertanksinfo;
	}
	/**
	 * 事件发生
	 */
	public boolean ishappen(){
		boolean happen=false;
		int serk=0;
		for(int i=0;i<allothertanksinfo.size();i++){
			//我的基座接触到了粒子系统的最近的粒子,则认为被烧到了,
			ParticleMesh particlesys=(ParticleMesh)allothertanksinfo.get(i).getfiregun().getChild("fire");
			Vector3f parposition;
			float distance=1000;
			int particlenum=particlesys.getNumParticles();
			for(int j=particlenum*2/5;j<particlenum*3/5;j++){
				parposition=particlesys.getParticle(j).getPosition();
				if(parposition.distance(info.getLocaltranslation())<distance){
			         distance=parposition.distance(info.getLocaltranslation()); 
			         serk=i;
				}
			}
			TankInformation hitmetank=allothertanksinfo.get(serk);
			if(!hitmetank.getendfireflag()&&distance<1f){
				//对方加分
				hitmetank.setScore(hitmetank.getScore()+0.5f);
				happen=true;
			}
		}
		return happen;
	}
}
